package fabriques;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import donnees.*;

public class FabCommande {
	public static FabCommande fc;

	private static PreparedStatement pcreer;
	private static String creer = "insert into Commande values(?,?,?)";
	private static PreparedStatement prechercher;
	private static String rechercher = "select * from Commande where idCommande=?";
	private static PreparedStatement prechercherCommandeDeLaLigne;
	private static String rechercherCommandeDeLaLigne= "select idCommande from LigneCommande where idLigneCommande=?";
	private static PreparedStatement prechercherLesCommandesDuClient;
	private static String rechercherLesCommandesDuClient= "select idCommande from Commande where idClient=?";
	private static PreparedStatement pIDMax;
	private static String IDMax = "select max(idCommande) from Commande";
	private static PreparedStatement plister;
	private static String lister = "select * from Commande";
	private static PreparedStatement psupprimer;
	private static String supprimer = "delete from Commande where idCommande=?";
	
	public static synchronized FabCommande getInstance(){
		if(fc == null)
			fc = new FabCommande();
		return fc;
	}

	public static void setConnection(Connection c){
		try {
			pcreer = c.prepareStatement(creer);
			prechercher = c.prepareStatement(rechercher);
			plister = c.prepareStatement(lister);
			psupprimer = c.prepareStatement(supprimer);
			prechercherCommandeDeLaLigne = c.prepareStatement(rechercherCommandeDeLaLigne);
			prechercherLesCommandesDuClient = c.prepareStatement(rechercherLesCommandesDuClient);
			pIDMax = c.prepareStatement(IDMax);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private FabCommande(){
		Connexion.getInstance();
		Connection con = Connexion.getCon();
		setConnection(con);
	}

	public Commande creer(Date date, int idClient){
		int idPM = 0;
		/* on r�cup�re le dernier id afin de cr�er un nouvel ID coh�rent pour la base. */
		ResultSet res = null;
		try {
			res = pIDMax.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(res.next())
				idPM = res.getInt(1);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		idPM++;
		java.sql.Date dSQL = new java.sql.Date(date.getTime());
		try{
			pcreer.setInt(1,idPM);
			pcreer.setDate(2,dSQL);
			pcreer.setInt(3,idClient);
			pcreer.executeUpdate();
		}catch (SQLException e){
			e.printStackTrace();
		}
		return new Commande(idPM,date);
	}
	
	public Commande rechercher(int idCommande) throws SQLException{
		ResultSet res = null;
		try {
			prechercher.setInt(1, idCommande);
			res = prechercher.executeQuery();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		Date d = null;
		res.next();
		try{
		d = new Date(res.getDate(2).getTime());
		}catch(Exception e){
			System.out.println("attention probleme de transformation de date sql et util");
		}
		return new Commande(res.getInt(1),d);
	}
	
	public Iterator<Commande> lister(){
		ResultSet res= null;
		try {
			res=plister.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		List<Commande> lesCom = new ArrayList<Commande>();
		try {
			while(res.next()){
				lesCom.add(rechercher(res.getInt(1)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lesCom.iterator();
	}
	
	public boolean supprimer(int idCommande){
		int res=0;
		try {
			psupprimer.setInt(1, idCommande);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			res=psupprimer.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(res != 0)
			return true;
		return false;
	}
	
	public Commande rechercherCommandeDeLaLigne(int id) {
		ResultSet res = null;
		try {
			prechercherCommandeDeLaLigne.setInt(1, id);
			res = prechercherCommandeDeLaLigne.executeQuery();
		}catch (SQLException e) {
			e.printStackTrace();
		}

		try {
			if(res.next())
				return rechercher(res.getInt(1));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<Commande> rechercherLesCommandesDuClient(int id) {
		try{
			prechercherLesCommandesDuClient.setInt(1,id);
		}catch (SQLException e) {
			e.printStackTrace();
		}
		ResultSet res = null;
		try{
			res = prechercherLesCommandesDuClient.executeQuery();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		
		ArrayList<Commande> lesComm = new ArrayList<Commande>();
		try {
			while(res.next()){
				lesComm.add(rechercher(res.getInt(1)));
			}
			return lesComm;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
		
	}

}
